package asso.moretomore;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import asso.onetomore.Children;
import asso.onetomore.Mum;
import util.DBPoolUtils;

public class StudentDaoImpl implements StudentDao{

	@Override
	public Student selectById(int id)  {
		try {
			Connection c = DBPoolUtils.getConnection();
			String sql= "select stuid,sname,lesid,lname from student left join stules using(stuid) left join lesson using(lesid) where stuid=?";
			ResultSet set = DBPoolUtils.query(c, sql, id);
			if(set.next()) {
				Student s = new Student();
				s.setStuId(set.getInt(1));
				s.setsName(set.getString(2));
				
				List<Lesson> ls = new ArrayList<>();
				do{
					Lesson les = new Lesson();
					les.setLesId(set.getInt(3));
					les.setlName(set.getString(2));
					
					ls.add(les);
				}while(set.next());
				
				s.setLessons(ls);
				return s;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
